//
//  WXPAuthSelectView.m
//  内容选择框
//
//  Created by wxp on 2025/2/28.
//

#import "WXPAuthSelectView.h"

@implementation WXPAuthSelectView

#pragma mark - lazy
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.fontNum = 12;
        _labelTitle.numberOfLines = 0;
        _labelTitle.lineSpace = 6;
    }
    return _labelTitle;
}
- (UIButton *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIButton new];
        _viewBack.backgroundColor = [UIColor clearColor];
        _viewBack.tag = 1;
        [_viewBack addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _viewBack;
}
- (UIImageView *)imageArrow{
    if (_imageArrow == nil) {
        _imageArrow = [UIImageView new];
        _imageArrow.image = [UIImage imageNamed:@"icon-auth-select-arrow"];
        _imageArrow.widthHeight = XY(14, 14);
    }
    return _imageArrow;
}
- (UITextField *)tfContent{
    if (_tfContent == nil) {
        _tfContent = [UITextField new];
        _tfContent.textColor = COLOR_BLACK;
        // 设置占位符
        NSString *placeholderText = @"seleccione por favor";
        UIColor *placeholderColor = [UIColor lightGrayColor]; // 自定义颜色（例如浅灰色）
        NSDictionary *attributes = @{
            NSForegroundColorAttributeName: placeholderColor // 颜色属性
        };
        NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:attributes];
        _tfContent.attributedPlaceholder = attributedPlaceholder;
        
        _tfContent.font = [UIFont boldSystemFontOfSize:(14)];
        _tfContent.userInteractionEnabled = false;
        [_tfContent addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _tfContent;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH-2*LEFT_MARDIN;
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.labelTitle];
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageArrow];
    [self.viewBack addSubview:self.tfContent];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
        {
            if(self.blockSelect){
                self.blockSelect();
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 实现监听方法
- (void)textFieldDidChange:(UITextField *)textField{
    if(self.blockTextFieldDidChange){
        self.blockTextFieldDidChange();
    }
}

#pragma mark - 刷新view
- (void)resetViewWithModel:(ModelBtn *)model{
    self.viewBack.widthHeight = XY(SCREEN_WIDTH-4*LEFT_MARDIN, Ratio(44));
    [GlobalMethod setRoundView:self.viewBack color:COLOR_LINE numRound:16 width:1];
    
    [self.labelTitle fitTitle:model.title variable:self.viewBack.width];
    self.labelTitle.leftTop = XY(LEFT_MARDIN, Ratio(20));
    
    self.viewBack.leftTop = XY(self.labelTitle.left, self.labelTitle.bottom+Ratio(15));
    
    self.imageArrow.rightCenterY = XY(self.viewBack.width-LEFT_MARDIN, self.viewBack.height/2);
    
    if(isStr(model.content)){
        self.tfContent.placeholder = readStr(model.content);
    }
    self.tfContent.widthHeight = XY(self.imageArrow.left-2*LEFT_MARDIN, self.viewBack.height-(10));
    self.tfContent.leftCenterY = XY(LEFT_MARDIN, self.viewBack.height/2);
    
    self.height = self.viewBack.bottom;
}

@end
